<?php

use App\Libraries\Ffmpeg;

if (!function_exists('getSysRandCode')) {
    /**
     * 获取系统随机数32位
     * @param $uid
     * @return mixed|string
     */
    function getSysRandCode($uid)
    {
        try {
            $sys_rand_code = exec('uuidgen -r', $output, $status);
            $sys_rand_code = str_replace('-', '', $sys_rand_code);
        } catch (\Exception $e) {
            $uid_tag = str_pad($uid % 100, 2, "0", STR_PAD_LEFT);
            $sys_rand_code = $uid_tag . substr(uniqid(), 10) . substr(microtime(), 2, 8) . rand(100, 999);
            $sys_rand_code = md5($sys_rand_code);
        }
        return $sys_rand_code;
    }
}


if (!function_exists('showError'))
{
    function showError($code , $data = '',$msg = '')
    {
        return \App\Libraries\Error::format($code, $data, $msg);
    }
}

if (!function_exists('getRandCodeFileName'))
{
    function getRandCodeFileName($uid , $extension)
    {
        $file_name = getSysRandCode($uid) .'.'. $extension;
        return $file_name;
    }
}

if (!function_exists('seconds2Hms'))
{
    function seconds2Hms($seconds,$str = false)
    {
        $seconds = intval($seconds);
        if ($seconds >= 3600) {
            $h = floor($seconds / 3600);
            $seconds -= $h * 3600;
        } else {
            $h = '00';
        }

        if ($seconds >= 60) {
            $m = floor($seconds / 60);
            $seconds -= $m * 60;
        } else {
            $m = '00';
        }
        $h = strlen($h) == 1 ? ('0' . $h) : $h;
        $m = strlen($m) == 1 ? ('0' . $m) : $m;
        $seconds = strlen(intval($seconds)) == 1 ? ('0' . $seconds) : $seconds;
        if($str)
        {
            $result = $h > 0 ? intval($h).'小时' : '';
            $result .= $m > 0 ? intval($m).'分钟' : '';
            $result .= $seconds > 0 ? intval($seconds).'秒' : '';
            return $result;
        }
        else
        {
            return $h . ':' . $m . ':' . $seconds;
        }
    }
}

if (!function_exists('getPlaytime'))
{
    function getPlaytime($audio)
    {
        $playtime = 0;
        if(!empty($audio))
        {
            $play_audio_file_data = Ffmpeg::getInfo($audio);
            if($play_audio_file_data['status'])
            {
                $play_audio_file_time = explode(":",$play_audio_file_data['data']['play_time']);
                $audio_play_time = $play_audio_file_time[0] * 3600 + $play_audio_file_time[1] * 60 + $play_audio_file_time[2];
                $playtime = round($audio_play_time);
            }
        }

        return $playtime;
    }
}

if (!function_exists('addExt'))
{
    function addExt($data,\Illuminate\Http\Request $request)
    {
        $rtn = [];
        if(is_array($data))
        {
            foreach ($data as $key)
            {
                $rtn[$key] = isset($request->$key)?$request->$key:'';
            }
        }

        return json_encode($rtn);
    }
}
if (!function_exists('getExt'))
{
    function getExt($key,$json)
    {
        $rtn = '';
        if($key && $json)
        {
            $data = json_decode($json);

            $rtn = !empty($data->$key)?$data->$key:'';
        }

        return $rtn;
    }
}